import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/auth.config';
import { prisma } from '@/lib/prisma';
import { redirect } from 'next/navigation';
import { DestinationCard } from '../components/DestinationCard';
import Link from 'next/link';
import { unstable_noStore as noStore } from 'next/cache';

// 禁用页面缓存
export const dynamic = 'force-dynamic';

async function getMyDestinations() {
  // 禁用缓存
  noStore();
  
  const session = await getServerSession(authOptions);
  if (!session?.user?.id) return [];

  return prisma.destination.findMany({
    where: {
      createdById: session.user.id,
      isOfficial: false,
    },
    include: {
      savedBy: {
        select: {
          id: true,
          name: true,
        },
      },
      _count: {
        select: {
          activities: true,
        },
      },
    },
    orderBy: {
      createdAt: 'desc',
    },
  });
}

export default async function MyDestinationsPage() {
  const session = await getServerSession(authOptions);
  if (!session?.user) {
    redirect('/api/auth/signin');
  }

  const destinations = await getMyDestinations();

  return (
    <div className="max-w-7xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
      <div className="flex justify-between items-center mb-8">
        <h1 className="text-3xl font-bold text-blue-900">我的目的地</h1>
        <Link
          href="/destinations/new"
          className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700"
        >
          创建目的地
        </Link>
      </div>

      {destinations.length === 0 ? (
        <div className="text-center py-12 bg-gray-50 rounded-lg">
          <h3 className="mt-2 text-sm font-medium text-gray-900">暂无目的地</h3>
          <p className="mt-1 text-sm text-gray-500">开始创建您的第一个目的地吧</p>
          <div className="mt-6">
            <Link
              href="/destinations/new"
              className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700"
            >
              创建目的地
            </Link>
          </div>
        </div>
      ) : (
        <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
          {destinations.map((destination) => (
            <DestinationCard
              key={destination.id}
              destination={destination}
              showActions={true}
            />
          ))}
        </div>
      )}
    </div>
  );
} 